package com.cdtu.controller;

import com.cdtu.entity.SysAttendance;
import com.cdtu.entity.SysEmployee;
import com.cdtu.entity.SysSalary;
import com.cdtu.service.SysAttendanceService;
import com.cdtu.service.SysEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/attendance")
//解决跨域问题
@CrossOrigin
public class SysAttendanceController extends BaseController{

    @Autowired
    private SysAttendanceService sysAttendanceService;

    @Autowired
    private SysEmployeeService sysEmployeeService;

    @RequestMapping("/list")
    public ModelAndView attendanceList(ModelAndView modelAndView){
        List<SysAttendance> list = sysAttendanceService.list();
        modelAndView.addObject("attendanceList",list);
        List<SysEmployee> list1 = sysEmployeeService.list();
        modelAndView.addObject("empList",list1);
        modelAndView.setViewName("system/sysAttendance/attendanceList");
        return modelAndView;
    }
    @RequestMapping("/toAddUI")
    public ModelAndView toAddSysAttendance(ModelAndView modelAndView) {

        List<SysEmployee> list = sysEmployeeService.list();
        //返回数据
        modelAndView.addObject("empList", list);

        //2.跳转页面;
        modelAndView.setViewName("system/sysAttendance/addSysAttendance");
        return modelAndView;
    }

    @RequestMapping("/addAttendance")
    public ModelAndView addEmpUI(SysAttendance sysAttendance, ModelAndView modelAndView) {



        sysAttendance.setCreated(new Date());

        sysAttendanceService.save(sysAttendance);
        //2.跳转页面;
        modelAndView.setViewName("redirect:/attendance/list");
        return modelAndView;
    }

    @RequestMapping("/delete/{id}")
    public ModelAndView delete(@PathVariable("id") Long id, ModelAndView modelAndView) {

        boolean remove = sysAttendanceService.removeById(id);
        modelAndView.setViewName("redirect:/attendance/list");
        return modelAndView;
    }

    @RequestMapping("/toUpdateUI/{id}")
    public ModelAndView toUpdateUI(ModelAndView modelAndView,@PathVariable("id") Long id) {

        SysAttendance sysAttendance = sysAttendanceService.getById(id);
        //返回数据
        modelAndView.addObject("sysAttendance", sysAttendance);

        //2.跳转页面;
        modelAndView.setViewName("system/sysAttendance/updateSysAttendance");
        return modelAndView;
    }

    @RequestMapping("/update")
    public ModelAndView update(SysAttendance sysAttendance,ModelAndView modelAndView){

        boolean b = sysAttendanceService.updateById(sysAttendance);


        modelAndView.setViewName("redirect:/attendance/list");
        return modelAndView;
    }
}
